package com.andy.qhb.controller;

import java.net.URLEncoder;
import java.util.Base64;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.andy.qhb.base.RequestUtil;
import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.entity.UsrPayLog;
import com.andy.qhb.exception.GoSeviceException;
import com.andy.qhb.game.GameConf;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.heepay.SmallTools;


/**
 * Created by Administrator on 2017/8/15.
 *
 * 声明：由于该Demo支持多种支付类型，所以参数可能多于某一种支付类型文档中所存在的参数
 *       如仅需一种支付类型，可自行对照文档删除无关参数
 *
 */
@Controller
@RequestMapping(value="game/api")
public class HeepayController extends GameController {

	//请求地址
	public static String url = "https://pay.heepay.com/Payment/Index.aspx";
	
	@RequestMapping("pay")  
	public String heepay(HttpServletRequest request, HttpSession session,String payType,String coin) throws Exception {
        /**
         * 支付类型选择
         * is_phone、is_frame、pay_type控制所有支付类型
         *
         * 微信PC扫码   is_phone=0 is_frame=0 pay_type=30
         * 微信H5       is_phone=1 is_frame=0 pay_type=30
         * 微信公众号   is_phone=1 is_frame=1 pay_type=30
         *
         * 支付宝PC扫码 is_phone=0 is_frame=0 pay_type=22
         * 支付宝H5     is_phone=1 is_frame=0 pay_type=22
         *
         * QQ PC扫码    is_phone=0 is_frame=0 pay_type=31
         * QQ H5        is_phone=1 is_frame=0 pay_type=31
         *
         * 京东PC扫码   is_phone=0 is_frame=0 pay_type=33
         *
         * 其他卡类支付 is_phone=0 is_frame=0 pay_type=参照文档
         */

		GameUser user = getUser(session);
		
		if(StringUtils.isEmpty(coin)) {
			throw new GoSeviceException("请输入金额");
		}
		
		Double.parseDouble(coin);
		
		String pay_type = "30";//支付类型
		String is_frame = "1";//是否是公众号
		if("2".endsWith(payType)) {
			pay_type = "22";
			is_frame = "0";
		}
        //请求参数
        String version = "1";//版本号
        String is_phone = "1";//是否是手机端
        String agent_bill_id = SmallTools.getDate("yyyyMMddHHmmss")+user.getId()+RandomUtils.nextInt(10000, 100000);//商户内部订单编号
        String pay_amt = coin;//订单金额
        String return_url = "http://www.60397n.cn/";//支付完成跳转地址
        String notify_url = "http://www.60397n.cn/game/api/heepayNotify";//支付完成异步通知地址
        String user_ip = RequestUtil.getRemoteIp();//用户IP地址
        String agent_bill_time = SmallTools.getDate("yyyyMMddHHmmss");//提交单据时间
        String goods_name = URLEncoder.encode("衣服","GBK");//商品名称
        String goods_num = "1";//产品数量
        String remark = "520";//备注
        String goods_note = URLEncoder.encode("白色","GBK");//支付说明
        String sign_type = "MD5";//签名方式
        String sign = "";//签名结果
        String meta_option = "{\"s\":\"WAP\",\"n\":\"京东官网\",\"id\":\"https://m.jd.com\"}";//详见开发文档

        //meta_option参数先Base64加密，再URLencode
        meta_option = URLEncoder.encode(Base64.getEncoder().encodeToString(meta_option.getBytes("gb2312")),"UTF-8");
        String sign1 = "version="+version+
                "&agent_id="+GameConf.agent_id+
                "&agent_bill_id="+agent_bill_id+
                "&agent_bill_time="+agent_bill_time+
                "&pay_type="+pay_type+
                "&pay_amt="+pay_amt+
                "&notify_url="+notify_url+
                "&return_url="+return_url+
                "&user_ip="+user_ip+
                "&key="+GameConf.heepay_key;

        System.out.println("签名参数："+sign1);
        //对签名参数进行MD5加密得到sign
        sign = SmallTools.MD5en(sign1);
        //拼接请求参数
        String parameter = "version="+version+
                "&is_phone="+is_phone+
                "&is_frame="+is_frame+
                "&pay_type="+pay_type+
                "&agent_id="+GameConf.agent_id+
                "&agent_bill_id="+agent_bill_id+
                "&pay_amt="+pay_amt+
                "&return_url="+return_url+
                "&notify_url="+notify_url+
                "&user_ip="+user_ip+
                "&agent_bill_time="+agent_bill_time+
                "&goods_name="+goods_name+
                "&remark="+remark+
                "&sign_type="+sign_type+
                "&goods_num="+goods_num+
                "&goods_note="+goods_note+
                "&meta_option="+meta_option+
                "&sign="+sign;
        System.out.println("请求参数："+parameter);
        
        UsrPayLog payLog = new UsrPayLog();
        payLog.setBillId(agent_bill_id);
        payLog.setJson(parameter);
        payLog.setStatus(0);
        payLog.setUid(user.getId());
        payLog.setType(0);
        DaoUtils.insert(payLog);
        
        //携带请求参数并重定向跳转下单地址
        return "redirect:"+url+"?"+parameter;
    }
	
}
